<?php
namespace PsiFramework\Classes\Services;

import("Classes.Util.UI.Response");
import("Classes.Services.Format.JsonFormat");

use \PsiFramework\Classes\Services\Format\JsonFormat;
use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Util\UI\Response;
use \PsiFramework\Classes\Util\UI\TemplateManager;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe abstraite de services
 *
 * @package Services
 */
abstract class AbstractService
{
    /**
     * @var Request
     */
    protected /*Request*/         $_request ;
    /**
     * @var Response
     */
    protected /*Response*/        $_response ;
    /**
     * @var string
     */
    protected /*string*/          $_value ;
    /**
     * @var TemplateManager
     */
    protected /*TemplateManager*/ $_templateManager ;
    /**
     * @var string
     */
    protected /*string*/          $_format ;

    /**
     * AbstractService constructor
     */
    public function __construct()
    {
        $this->_response = new Response();
        $this->_request = Request::getInstance();
        $this->_templateManager = TemplateManager::getInstance();
    }

    /**
     * The main function of the method which is called every time
     * a service is created by the front controller.
     *
     * @param Request $request The HTTP request to be treated by the action
     * 
     * @return void
     */
    public function execute(Request $request, /*string*/ $defaultFormat='xml')
    {
        $this->_value = $request->getParameter('value');
        $format = $request->getParameter("format");
        if (isNullOrEmptyString($format)) {
            $format = $defaultFormat ;
        }
        $this->_format = $format ;
    }

    /**
     * Shows the service response thanks to a template
     *
     * @param $template    the template to show
     */
    public function show(/*string*/ $template="xml")
    {
        $this->_templateManager->addTemplate($template, $this->_response);
    }
}
